package com.xxx.video.commen.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.BatteryManager;
import android.util.Log;

import com.xxx.video.VideoApplication;


public class AppUtils {
    private static AppUtils mAppUtils = null;

    private AppUtils() {
    }

    public static AppUtils getInstance() {
        if (mAppUtils == null) {
            synchronized (AppUtils.class) {
                if (mAppUtils == null) {
                    mAppUtils = new AppUtils();
                }
            }
        }
        return mAppUtils;
    }

    /**
     * get version name
     *
     * @return name
     */
    public String getVersionName() {
        try {
            PackageManager packageManager = VideoApplication.getInstance().getPackageManager();
            PackageInfo packInfo = packageManager.getPackageInfo(VideoApplication.getInstance().getPackageName(), 0);
            String version = packInfo.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * get version code
     *
     * @return code
     */
    public int getVersionCode() {
        try {
            PackageManager packageManager = VideoApplication.getInstance().getPackageManager();
            PackageInfo packInfo = packageManager.getPackageInfo(VideoApplication.getInstance().getPackageName(), 0);
            int version = packInfo.versionCode;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * get battery
     */
    public int getBattery() {
        BatteryManager manager = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            manager = (BatteryManager) VideoApplication.getInstance().getSystemService(Context.BATTERY_SERVICE);
            int battery = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
            return battery;
        }
        return 100;
    }

    /**
     *  is wifi
     * @return is wifi
     */
    public boolean isWifi() {
        ConnectivityManager connectivityManager = (ConnectivityManager) VideoApplication.getInstance()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
            Log.d("network",activeNetInfo.toString());
            if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return true;
            }
        }
        return false;
    }
}
